package com.ws.common.test;

import java.util.LinkedList;

public class 多线程练习 {

    public static void main(String args[]){
        篮子 lan=new 篮子();
        添加 a1=new 添加(lan);
        添加 a2=new 添加(lan);
        删除 b1=new 删除(lan);
        Thread add1=new Thread(a1);
        Thread add2=new Thread(a2);
        Thread delete1=new Thread(b1);
        add1.start();
        add2.start();
        delete1.start();
    }

}

class 篮子{
    LinkedList<Integer> list=new LinkedList<>();
    Object addb=new Object();
    Object deleteb=new Object();
    public synchronized void add(Integer i){
        try {
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        if(list.size()>=5){
            try {
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.add(i);
        notify();
        System.out.println(list.size()+"      "+i);
    }

    public synchronized void delete(){

        notifyAll();
    }
}

class 添加 implements Runnable{
    篮子 lanzi;
    添加 (篮子 lanzi){
        this.lanzi=lanzi;
    }
    @Override
    public void run() {
        for (int i=0;i<10;i++) lanzi.add(i);
    }
}

class 删除 implements Runnable{
    篮子 lanzi;
    删除 (篮子 lanzi){
        this.lanzi=lanzi;
    }
    @Override
    public void run() {
         lanzi.delete();
    }
}